package com.simple.accounting.activity;

import android.annotation.SuppressLint;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.simple.accounting.R;
import com.simple.accounting.db.AccountBean;
import com.simple.accounting.db.DBManager;
import com.simple.accounting.db.TypeBean;
import com.simple.accounting.frag_record.TypeBaseAdapter;
import com.simple.accounting.utils.AlterBeiZhuDialog;
import com.simple.accounting.utils.KeyBoardUtils;
import com.simple.accounting.utils.SelectTimeDialog;
import com.simple.accounting.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class AlterActivity extends BaseActivity implements View.OnClickListener{

    private KeyboardView keyboardView;
    private EditText moneyEditText;
    private ImageView typeImageView;
    private TextView titleTv,typeTv,remarksTv,timeTv;
    private GridView typeGv;
    private List<TypeBean> typeList;
    private TypeBaseAdapter mTypeBaseAdapter;
    private AccountBean accountBean;
    int type;//类型  收入---1   支出---0

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter);
        initView();
        loadDataToGV();
        setGVListener(); //设置GridView每一项的点击事件
        initData();
    }

    private void initView() {
        keyboardView = findViewById(R.id.frag_record_keyboard);
        moneyEditText = findViewById(R.id.frag_record_et_money);
        typeImageView = findViewById(R.id.frag_record_iv);
        typeGv = findViewById(R.id.frag_record_gv);
        titleTv = findViewById(R.id.tv_title);
        typeTv = findViewById(R.id.frag_record_tv_type);
        remarksTv = findViewById(R.id.frag_record_tv_beizhu);
        timeTv = findViewById(R.id.frag_record_tv_time);
        remarksTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
    }

    private void initData(){
        accountBean = (AccountBean)getIntent().getSerializableExtra("data");
        type = accountBean.getKind();
        titleTv.setText(type == 0 ? "修改支出" : "修改收入");
        typeImageView.setImageResource(accountBean.getsImageId());
        typeTv.setText(accountBean.getTypename());
        moneyEditText.setText(accountBean.getMoney()+"");
        remarksTv.setText(accountBean.getRemarks());
        timeTv.setText(accountBean.getTime());

        List<TypeBean> list = DBManager.getTypeList(type);
        typeList.addAll(list);
        mTypeBaseAdapter.updateData(typeList);
        mTypeBaseAdapter.setSelectedPosition(accountBean.getTypename());

        //让自定义软键盘显示出来
        KeyBoardUtils boardUtils = new KeyBoardUtils(keyboardView, moneyEditText);
        boardUtils.showKeyboard();
        //设置接口，监听确定按钮按钮被点击了
        boardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener() {
            @Override
            public void onEnsure() {
                //获取输入钱数
                String moneyStr = moneyEditText.getText().toString();
                float money = Float.parseFloat(moneyStr);
                if (TextUtils.isEmpty(moneyStr)||money<=0) {
                    ToastUtils.show(AlterActivity.this,"输入的金额不能为0");
                    //finish();
                    return;
                }

                accountBean.setMoney(money);
                //获取记录的信息，保存在数据库当中
                saveAccountToDB();
                // 返回上一级页面
                finish();
            }
        });

    }

    public void saveAccountToDB(){
        DBManager.updateItemToAccountTb(accountBean);
    }

    /* 给GridView填充数据*/
    public void loadDataToGV() {
        typeList = new ArrayList<>();
        mTypeBaseAdapter = new TypeBaseAdapter(this, typeList);
        typeGv.setAdapter(mTypeBaseAdapter);
    }

    /* 设置GridView每一项的点击事件*/
    private void setGVListener() {
        typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mTypeBaseAdapter.updatePosition(position);
                TypeBean typeBean = typeList.get(position);
                String typename = typeBean.getTypename();
                typeTv.setText(typename);
                accountBean.setTypename(typename);
                int sImageId = typeBean.getSimageId();
                typeImageView.setImageResource(sImageId);
                accountBean.setsImageId(sImageId);
            }
        });
    }

    /* 获取当前时间，显示在timeTv上*/
    private void setInitTime() {
        Date date = new Date();
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time = sdf.format(date);
        timeTv.setText(time);
        accountBean.setTime(time);

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        accountBean.setYear(year);
        accountBean.setMonth(month);
        accountBean.setDay(day);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.frag_record_tv_time:
                showTimeDialog();
                break;
            case R.id.frag_record_tv_beizhu:
                String s = remarksTv.getText().toString();
                showBZDialog(s);
                break;
        }
    }

    /* 弹出显示时间的对话框*/
    private void showTimeDialog() {
        SelectTimeDialog dialog = new SelectTimeDialog(this, accountBean.time);
        dialog.show();
        //设定确定按钮被点击了的监听器
        dialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String time, int year, int month, int day) {
                timeTv.setText(time);
                accountBean.setTime(time);
                accountBean.setYear(year);
                accountBean.setMonth(month);
                accountBean.setDay(day);
            }
        });
    }



    /* 弹出备注对话框*/
    public  void showBZDialog(String s){
        final AlterBeiZhuDialog dialog = new AlterBeiZhuDialog(this);
        dialog.show();
        dialog.et.setText(s);
        dialog.et.setSelection(s.length());//将光标定位在文本的最后面
        dialog.setDialogSize();
        dialog.setOnEnsureListener(new AlterBeiZhuDialog.OnEnsureListener() {
            @Override
            public void onEnsure() {
                String msg = dialog.getEditText();
                remarksTv.setText(msg);
                accountBean.setRemarks(msg);
                dialog.cancel();
            }
        });
    }
}
